package com.example.reference_book.controller;

import com.example.reference_book.entity.Course;
import com.example.reference_book.entity.CourseInfo;
import com.example.reference_book.service.CourseService;
import com.example.reference_book.util.CourseSearchUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程表(Course)表控制层
 *
 * @author makejava
 * @since 2022-06-07 22:19:03
 */
@RestController
@RequestMapping("course")
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    @Resource
    private SearchCourseUtil searchCourseUtil;

    private CourseSearchUtil searchUtil = CourseSearchUtil.getSearchUtil();

    @GetMapping("/searchCourse/{type}/{key}")
    public List<Course> searchByKey(@PathVariable("type") String type, @PathVariable("key") String key){
        return this.searchCourseUtil.search(type,key);
    }

    @GetMapping("/searchByKey/{type}/{key}")
    public ResponseEntity<List<CourseInfo>> searchByKey(@PathVariable("type") Integer type, @PathVariable("key") String key){
        return ResponseEntity.ok(this.searchUtil.searchByKey(type,key));
    }

    /**
     * 查询所有数据
     */
    @PostMapping("/selectAll")
    public ResponseEntity<List<CourseInfo>> selectAll() {
        List<CourseInfo> list = this.courseService.selectAll();
        searchUtil.init(list);
        return ResponseEntity.ok(list);
    }

    @PostMapping("/selectByDeptId")
    public ResponseEntity<List<Course>> selectByDeptId(int id) {
        return ResponseEntity.ok(this.courseService.selectByDeptId(id));
    }

    /**
     * 分页查询
     *
     * @param course 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Course>> queryByPage(Course course, PageRequest pageRequest) {
        return ResponseEntity.ok(this.courseService.queryByPage(course, pageRequest));
    }

    @GetMapping("{id}")
    public ResponseEntity<Course> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.courseService.queryById(id));
    }

    @ResponseBody
    public ResponseEntity<List<Course>> queryByName(String type,String key) {
//        System.out.println("search---2");
        return ResponseEntity.ok(this.courseService.queryByName(type,key));
    }

    /**
     * 新增数据
     *
     * @param course 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Course> add(Course course) {
        return ResponseEntity.ok(this.courseService.insert(course));
    }

    @PostMapping("/add")
    public ResponseEntity<Course> addCourse(Course course) {
        return ResponseEntity.ok(this.courseService.insert(course));
    }

    /**
     * 编辑数据
     *
     * @param course 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public ResponseEntity<Course> edit(Course course) {
        return ResponseEntity.ok(this.courseService.update(course));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.courseService.deleteById(id));
    }

}

